<template>
	<view class="l_pages" v-bind:class="hasShow=='none'?'bg':''" :style="skin">
		<jnavbar :title="'地址列表'" :PageId="11"></jnavbar>
		<scroll-view scroll-y class="page_container">
		<block v-if="hasShow == 'none'">
			<view class="uni-block-address uni-flex-column">
				<vimage class="image" imgtype="1" src="static/images/noadress.png" mode="widthFix"></vimage>
				<text class="sm-font">暂无收货地址</text>
			</view>
		</block>
		<block v-else-if="hasShow == 'list'">
			<u-gap height='38' bg-color='#f4f4f4'></u-gap>
			<view class="uni-block-list uni-flex-row uni-view" v-for="(item, i) in list" :key="i" @click="getAddress(item)">
				<view class="left">
					<view class="consignee"><text>{{ item.consignee }}</text><text>{{ item.iphone }}</text></view>
					<view class="address">{{ item.address.replace(/,/g, ' ') }}</view>
				</view>
				<view class="yline"></view>
				<view class="right">
					<text  @click.stop="goAddress(2,item.id)">编辑</text>
					<text @click.stop="delAddress(item.id)">删除</text>
				</view>
			</view>			
		</block>
		</scroll-view>
		<view class="uni-block-button">
			<u-button  :custom-style="customStyle" type="primary"  @click="goAddress(1)">新建地址</u-button>		
		</view>
		<nloading></nloading>
		<tab-bar ref="tabbar" :PageId="11"></tab-bar>
	</view>
</template>

<script>
import http from '@/common/http.js';

export default {
	onShareAppMessage() {
		return this.wxShare();
	},
	data() {
		return {
			list: [],
			hasShow: 'none',
			addressId: -1,
			customStyle:{
				"background": "var(--main-color)",
				"box-shadow": "-2px 5px 9px 0px var(--content-color)",
				"border-radius": "49rpx",
				"color": "#FFFFFF",
				"font-size":"34rpx",
				"height":"97rpx",
				"margin":"0 40rpx"
			},
			
		};
	},
	computed: {
		skin() {
			return this.$store.state.skin;
		}
	},
	async onLoad(param) {
		await this.$getWx
		if (param.addressId) {
			this.addressId = param.addressId;
		}
	},
	onShow() {
		const pages = getCurrentPages();
		const prevPage = pages[pages.length - 2];
		// delete prevPage.address
		this.getAddressList();
	},
	onPullDownRefresh() {
		this.getAddressList();
	},
	onUnload() {
		const _this = this;

		// const pages = getCurrentPages();
		// const prevPage = pages[pages.length - 2];

		// if (!prevPage.address) {
		// 	for (let i = 0; i < _this.list.length; i++) {
		// 		if (_this.list[i].id == _this.addressId) {
		// 			prevPage.address=this.list[i]
		// 			// prevPage.setData({
		// 			// 	address: _this.list[i]
		// 			// });
		// 			break;
		// 		}
		// 	}
		// }
	},
	methods: {
		//获得地址列表
		getAddressList() {
			this.changeloading({loading: true});
			const _this = this;

			http('/order/userAddressList', {}, 'GET').then(
				res => {
					const json = res.data.msg;
					uni.stopPullDownRefresh()
					_this.list = json.data;

					//判断地址列表是否为空
					if (json.data.length > 0) {
						_this.hasShow = 'list';
					} else {
						_this.hasShow = 'none';
					}
					this.changeloading({loading: false});

				},
				e => {
					uni.showModal({
						title: '提示',
						content: e,
						showCancel: false
					});
					uni.hideLoading();
				}
			);
		},
		//删除地址
		delAddress(id) {
			const _this = this;

			uni.showModal({
				title: '提示',
				content: '是否要删该地址？',
				success: function(res) {
					if (res.confirm) {
						// 请求接口
						// uni.showLoading({
						// 	title: '正在删除...',
						// 	mask: true
						// });

						http('/order/' + id, {}, 'DELETE').then(
							res => {
								
								_this.getAddressList();

							},
							e => {
								uni.showModal({
									title: '提示',
									content: e,
									showCancel: false
								});
							}
						);
					}
				}
			});
		},
		//设置默认地址
		setDefault(item) {
			//若有addressId则是订单页进入 不可设置默认
			if (this.addressId >= 0) {
				return;
			}

			const _this = this;
			const param =item
			param.type==2

			http('/order/saveUserAddress', param, 'PUT').then(
				res => {
				//刷新地址列表
				_this.getAddressList();				
				},
				e => {
					uni.showModal({
						title: '提示',
						content: e,
						showCancel: false
					});
				}
			);
		},
		//获取订单地址
		getAddress(item) {
			//判断是否由订单页进入
			if (this.addressId >= 0) {
				const pages = getCurrentPages();
				const prevPage = pages[pages.length - 2];
				// #ifdef MP-WEIXIN
				prevPage.setData({
					address: item
				});
				// #endif
				// #ifndef MP-WEIXIN
				prevPage.address=item
				// #endif
				// 
			
				uni.navigateBack({
					delta: 1
				});
			} else {
				return;
			}
		},
		//地址详情页
		goAddress(type,id) {
			if (type==2) {
				this.Jumpurl({
					link:'../mine/address/detail?id=' + id,
					type:1,
					login_type:2
				})
				//编辑
			} else {
				//新增
				this.Jumpurl({
					link:'../mine/address/detail',
					type:1,
					login_type:2
				})
			}
		}
	}
};
</script>

<style lang="scss">
	.scroll_div{
		height: calc(100vh - 151rpx);
		
	}
	.bg{
		background: #FFFFFF;
	}
.uni-block-address {
	padding-top: 192rpx;
	align-items: center;
	.image {
		width: 358rpx;
		margin-bottom: 44rpx;
		// width: 50%;
		// height: 300upx;
		// margin-bottom: 100upx;
	}
	text {
		color: #FF5910;
		font-size: 32rpx;
	}
}
.uni-block-list {
	width: 692rpx;
	height: 192rpx;
	margin: 0 auto;
	margin-bottom: 20rpx;
	background: #FFFFFF;
	border-radius: 10rpx;
	padding: 0 30rpx;
	.left{
		width: 460rpx;
		.consignee{
			text:first-child{				
				font-size: 36rpx;
				font-weight: 500;
				color: #333333;
			}
			text:last-child{

				font-size: 28rpx;
				font-weight: 500;
				color: #999999;
				margin-left: 14rpx;
			}
		}
		.address{	
			margin-top: 10rpx;
			font-size: 26rpx;
			font-weight: 500;
			color: #333333;
			line-height: 40rpx;
		}
	}
	.yline{
		width: 2rpx;
		height: 96rpx;
		background: #CDCDCD;
	}
	.right{
		text{		
			font-size: 26rpx;
			font-weight: 500;
			color: #999999;
			margin-left: 30rpx;
		}
	}
}

.uni-block-button {
	// margin-top: 20upx;
	height: 151rpx;
	// display: flex;
	// align-items: center;
	.btns{
		
		// margin-top: 27rpx
	}

}
.hoverclass{
	background-color: #000000;
}
</style>
